<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"  width="60%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="140px">
      <el-form-item label="真实姓名" prop="name">
        <el-input v-model="dataForm.name" placeholder="真实姓名"></el-input>
      </el-form-item>
      <el-form-item label="参赛单元" prop="unit">
        <el-select v-model="dataForm.unit" placeholder="请选择">
          <el-option
            v-for="item in unitTypes"
            :key="item.label"
            :label="item.label"
            :value="item.type">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="联系电话" prop="tel">
        <el-input v-model="dataForm.tel" placeholder="联系电话"></el-input>
      </el-form-item>
      <el-form-item label="电子邮件" prop="email">
        <el-input v-model="dataForm.email" placeholder="电子邮件"></el-input>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="地址"></el-input>
      </el-form-item>
      <el-form-item label="作品名称" prop="workName">
        <el-input v-model="dataForm.workName" placeholder="作品名称"></el-input>
      </el-form-item>
      <el-form-item label="作品简介" prop="workDesc">
        <el-input type="textarea" v-model="dataForm.workDesc" placeholder="作品简介"></el-input>
      </el-form-item>
      <el-form-item label="作品链接" prop="workLink">
        <el-input v-model="dataForm.workLink" placeholder="作品链接"></el-input>
      </el-form-item>
      <el-form-item label="上传时间" prop="createTime">
        <el-date-picker
          v-model="dataForm.createTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="累计票数" prop="voteNum">
        <el-input v-model="dataForm.voteNum" placeholder="累计票数"></el-input>
      </el-form-item>

      <el-form-item label="排序位置" prop="voteNum">
        <el-input v-model="dataForm.sortNo" placeholder="排序位置"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import API from '@/api'
  export default {
    data () {
      return {
        visible: false,
        unitTypes: [{
          type: 1,
          label: '短片'
        }, {
          type: 2,
          label: '短视频'
        }],
        dataForm: {
          id: 0,
          name: '',
          unit: '',
          tel: '',
          email: '',
          address: '',
          workName: '',
          sortNo: 100,
          workDesc: '',
          workLink: '',
          createTime: '',
          voteNum: ''
        },
        dataRule: {
          name: [
            { required: true, message: '真实姓名不能为空', trigger: 'blur' }
          ],
          unit: [
            { required: true, message: '参赛单元不能为空', trigger: 'blur' }
          ],
          tel: [
            { required: true, message: '联系电话不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '电子邮件不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '地址不能为空', trigger: 'blur' }
          ],
          workName: [
            { required: true, message: '作品名称不能为空', trigger: 'blur' }
          ],
          workDesc: [
            { required: true, message: '作品简介不能为空', trigger: 'blur' }
          ],
          workLink: [
            { required: true, message: '作品链接不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '上传时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            API.baseworkvote.info(this.dataForm.id).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.baseworkvote.name
                this.dataForm.unit = data.baseworkvote.unit
                this.dataForm.tel = data.baseworkvote.tel
                this.dataForm.email = data.baseworkvote.email
                this.dataForm.sortNo = data.baseworkvote.sortNo
                this.dataForm.address = data.baseworkvote.address
                this.dataForm.workName = data.baseworkvote.workName
                this.dataForm.workDesc = data.baseworkvote.workDesc
                this.dataForm.workLink = data.baseworkvote.workLink
                this.dataForm.createTime = data.baseworkvote.createTime
                this.dataForm.voteNum = data.baseworkvote.voteNum
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = {
              'id': this.dataForm.id || undefined,
              'name': this.dataForm.name,
              'unit': this.dataForm.unit,
              'tel': this.dataForm.tel,
              'email': this.dataForm.email,
              'sortNo': this.dataForm.sortNo,
              'address': this.dataForm.address,
              'workName': this.dataForm.workName,
              'workDesc': this.dataForm.workDesc,
              'workLink': this.dataForm.workLink,
              'createTime': this.dataForm.createTime,
              'voteNum': this.dataForm.voteNum
            }
            var tick = !this.dataForm.id ? API.baseworkvote.add(params) : API.baseworkvote.update(params)
            tick.then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
